package disk_rpc

import (
	"context"
	"disk/disk_rpc_server/disk_pb"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Delete(r *gin.RouterGroup) {
	r.POST("/delete/:id", func(c *gin.Context) {
		idStr := c.Param("id")
		id, err := strconv.Atoi(idStr)
		if err != nil {
			c.JSON(200, gin.H{
				"msg":  "id格式错误",
				"data": err.Error(),
				"code": 400,
			})
			return
		}

		client := NewClient()
		result, err := client.Delete(context.Background(), &disk_pb.DeleteReq{
			Id: int64(id),
		})
		if err != nil {
			c.JSON(200, gin.H{
				"msg":  "删除失败",
				"data": err.Error(),
				"code": 400,
			})
			return
		}

		if result.Code != 0 {
			c.JSON(200, gin.H{
				"msg":  "删除失败",
				"data": result.Message,
				"code": 400,
			})
		} else {
			c.JSON(200, gin.H{
				"msg":  "删除成功",
				"data": idStr,
				"code": 200,
			})
		}
	})
}
